const mongoose = require('../db/mongoose')

const Schema = mongoose.Schema

const studentSchema = new Schema({
	username: String,
	age: String,
	school: {
		type: Schema.Types.ObjectId,
		ref: 'school'
	}
})

const schoolSchema = new Schema({
	username: Schema.Types.String,
	students: [
		{
			type: Schema.Types.ObjectId,
			ref: 'students'
		}
	],
	city: {
		type: Schema.Types.ObjectId,
		ref: 'city'
	}
})

const citySchema = new Schema({
	username: String,
	school: [
		{
			type: Schema.Types.ObjectId,
			ref: 'school'
		}
	]
})

const Student = mongoose.model('student', studentSchema)
const School = mongoose.model('school', schoolSchema)
const City = mongoose.model('city', citySchema)

;(async () => {
	// Student.find({ name: 'Tom' })
	// .populate({
	//     path: 'school',
	//     populate: {
	//        path: 'city',
	//     }
	// })
	// .exec(function (err, data) {
	//    console.log(data);
	// });

	const city = await new City({
		username: '北京',
		school: []
	}).save()

	const school = await new School({
		name: 'Test',
		students: [],
		city: city._id
	}).save()

	var student = await new Student({
		name: 'Tom',
		age: 20,
		school: school._id
	}).save()

	console.log('student:', city, school, student)
})()

// ;(async () => {
// try {
// 注册
// const data = await User.create({
//   username:'devin-test6',
//   password: '321',
//   address: 0,
//   // createTime: 'N'
// })

// const data = await User.updateOne(
//   { username: 'devin-test5' },
//   { password: 'abc' }
// )

// const data = await User.deleteOne(
//   { username: 'devin-test5' })

// console.log('data:', data)

// const findDate = await User.find({
//   createTime: {
//     $gte: new Date('2021,7,26 00:00:00') ,
//     $lt:  new Date('2021,7,26 23:50:00') }
//   })
//   console.log('findDate:', new Date())
// } catch(err) {
//   console.error('data:------', err.message)
// }

// 登陆 // token
//  const loginRes = await User.find({ username:'devin-test2', password: '321', })
//  console.log('loginRes:', loginRes)

// 查询 condition 查询条件、pageTotal为每页显示条数、pageNum为分页数量、cb为回调函数

// let pageTotal = 5;
// let pageNum = 2;

// const loginRes = await User.find({
//   // username: /dev/
// })
// .skip(pageTotal * (pageNum-1)).limit(pageTotal).sort({_id: -1})
// // .where('updateTime').gt(17).lt(66);

// console.log('loginRes:', loginRes)
// // 模糊搜索

// const res = await User.create({
//   username: '测试账号6',
//   password: '123',
//   address: '上海市奉贤区',
//   updateTime: new Date()*1
// })

// const data = await User.insertMany(
//   // {
//   //   username: '测试账号3',
//   //   password: '123',
//   //   address: '上海市奉贤区',
//   //   updateTime: new Date()*1
//   // },
//   {
//     username: '测试账号7',
//     password: '123',
//     address: '上海市奉贤区',
//     updateTime: new Date()*1
//   },
//   {
//     username: '测试账号8',
//     password: '123',
//     address: '上海市奉贤区',
//     updateTime: Date.now
//   }
// )

// console.log('res:', data)

// })()
